package ar.com.quickmart.classes;

import java.text.SimpleDateFormat;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import ar.com.quickmart.R;

public class ComparedPricesAdapter extends BaseAdapter {

	private final Context mContext;
	private final List<ComparedPrices> mComparedPrices;
	
	public ComparedPricesAdapter(Context context, List<ComparedPrices> comparedPrices) {
		super();
		mContext = context;
		mComparedPrices = comparedPrices;
	}
	
	@Override
	public int getCount() {
		return mComparedPrices.size();
	}

	@Override
	public Object getItem(int position) {
		return mComparedPrices.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		// La vista que contiene el layout de la fila voy a devolver
		View row = convertView;
		
		ComparedPricesHolder holder = new ComparedPricesHolder();
			
		if(row == null) {
			
			LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.compared_prices_list_item_layout, parent, false);
			
			holder.supermarketName = (TextView) row.findViewById(R.id.supermarket_name);
			holder.price = (TextView) row.findViewById(R.id.product_price);
			
			row.setTag(holder);			
		}
		else {
					holder = (ComparedPricesHolder) row.getTag();
		}		

			holder.supermarketName.setText(mComparedPrices.get(position).getSuperMarket().getName());
			holder.price.setText("$" + String.format("%.2f", mComparedPrices.get(position).getTotal()) + " " );
			
		return row;
	}
	
	private static class ComparedPricesHolder {
		public TextView supermarketName;
		public TextView price;
	}

}
